<template>
  <div>
    <el-row type=flex>
      <el-col :span="16">
        <el-button @click="handleCreate" icon="el-icon-plus" type="primary">新建优惠券</el-button>
      </el-col>
      <el-col :span="8">
        <el-input placeholder="请输入优惠券名称" v-model="filterForm.title">
          <el-button @click="handleFilter" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-table :data="reward.list" stripe class="hui-mt-1">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form :model="scope.row" label-width="8em" label-position="left" inline class="hui-data-table-expand">
            <el-form-item label="优惠券标题:">{{ scope.row.title }}</el-form-item>
            <el-form-item label="优惠券类型:">
              <el-tag v-if="scope.row.cardType === 'CASH'" size="small">代金券</el-tag>
              <el-tag v-else-if="scope.row.cardType === 'DISCOUNT'" size="small">折扣券</el-tag>
              <el-tag v-else-if="scope.row.cardType === 'GIFT'" size="small">兑换券</el-tag>
              <el-tag v-else-if="scope.row.cardType === 'GROUPON'" size="small">团购券</el-tag>
              <el-tag v-else-if="scope.row.cardType === 'GENERAL_COUPON'" size="small">优惠券</el-tag>
              <el-tag v-else size="small">{{ scope.row.cardType }}</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="优惠券">
        <template slot-scope="scope">
          <div>{{ scope.row.title }}</div>
          <div>
            <el-tag v-if="scope.row.status === 0" size="small">微信审核中</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success" size="small">正常</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="info" size="small">已过期</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger" size="small">已删除</el-tag>
            <el-tag v-else-if="scope.row.status === 4" type="info" size="small">微信审核失败</el-tag>
            <el-tag v-else-if="scope.row.status === 5" type="info" size="small">卡券被商户删除</el-tag>
            <el-tag v-else type="danger" size="small">{{ scope.row.status }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="有效期">
        <template slot-scope="scope">
          <template v-if="scope.row.dateInfo.type === 'DATE_TYPE_FIX_TIME_RANGE'">
            <div>开始时间: {{ $moment.unix(scope.row.dateInfo.beginTimestamp).format("YYYY-MM-DD") }}</div>
            <div>结束时间: {{ $moment.unix(scope.row.dateInfo.endTimestamp).format("YYYY-MM-DD") }}</div>
          </template>
          <template v-else-if="scope.row.dateInfo.type === 'DATE_TYPE_FIX_TERM'">
            <div>领取后 {{ scope.row.dateInfo.fixedBeginTerm === 0 ? '当' : scope.row.dateInfo.fixedBeginTerm }} 天生效</div>
            <div>有效期 {{ scope.row.dateInfo.fixedTerm }} 天</div>
            <div v-if="scope.row.dateInfo.endTimestamp">统一过期时间: {{ $moment.unix(scope.row.dateInfo.endTimestamp).format("YYYY-MM-DD") }}</div>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="最后操作人">
        <template slot-scope="scope">
          <div>{{ scope.row.updateUser }}</div>
          <div>{{ $moment.unix(scope.row.updateTime).format("YYYY-MM-DD HH:mm:ss") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="310px">
        <template slot-scope="scope">
          <el-button @click="handleCheckStatus(scope.row)" type="success" size="mini" :disabled="scope.row.status === 2 || scope.row.status === 3 || scope.row.status === 4 || scope.row.status === 5" plain>查询微信审核状态</el-button>
          <el-button @click="handleEdit(scope.row)" type="primary" size="mini" :disabled="scope.row.status === 2 || scope.row.status === 3 || scope.row.status === 4 || scope.row.status === 5" plain>编辑</el-button>
          <el-button @click="handleDelete(scope.row)" :disabled="scope.row.status === 2 || scope.row.status === 3 || scope.row.status === 4 || scope.row.status === 5" type="danger" size="mini" plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页，start-->
    <el-pagination @current-change="handleCurrentChange" :current-page.sync="reward.pages" :page-sizes="[10]" :page-size="10" layout="total, sizes, prev, pager, next" :total="reward.total" background class="hui-mt-2"></el-pagination>
    <!--分页，end-->
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterForm: {
        title: ''
      },
      loading: false,
      reward: {
        pages: 0,
        total: 0,
        list: []
      }
    }
  },
  created () {
    this.mockData()
  },
  methods: {
    // 筛选事件
    handleFilter () {
      if (this.filterForm.title === '') {
        return false
      } else {
        this.loading = true
        console.log(this.filterForm)
        this.$loading()
        setTimeout(() => {
          this.$loading().close()
          this.loading = false
        }, 500)
      }
    },
    // 新建事件
    handleCreate () {
      this.$router.push({
        path: 'detail',
        query: {
          type: 'create'
        }
      })
    },
    handleCheckStatus (row) {
      this.$loading()
      setTimeout(() => {
        this.$loading().close()
      }, 500)
    },
    // 编辑事件
    handleEdit (row) {
      let id = row.id
      this.$router.push({
        path: 'detail',
        query: {
          type: 'edit',
          id: id
        }
      })
    },
    // 删除事件
    handleDelete (row) {
      this.$confirm('确认删除这个优惠券么？删除后，本批次已经发给用户的券仍可继续使用，但无法再发新的券。', '删除确认', {
        confirmButtonText: '下线',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('下线, 优惠券ID:' + row.id)
        this.$message({
          type: 'success',
          message: '优惠券已删除!'
        })
      }).catch(() => {})
    },
    // 分页事件
    handleCurrentChange () {
      console.log(this.reward.pages)
      this.$loading()
      setTimeout(() => {
        this.$loading().close()
      }, 500)
    },
    // 模拟数据
    mockData () {
      let pages = this.reward.pages
      let total = 100
      let list = []
      let i = 10
      while (i > 0) {
        let _startTime = (val) => {
          return parseInt(this.$moment().day(val).set({'hour': 0, 'minute': 0, 'second': 0}).format('X'))
        }
        let _endTime = (val) => {
          return parseInt(this.$moment().day(val).set({'hour': 23, 'minute': 59, 'second': 59}).format('X'))
        }
        let _status = (min, max) => {
          min = Math.ceil(min)
          max = Math.floor(max)
          return Math.floor(Math.random() * (max - min + 1)) + min
        }
        let _updateTime = () => {
          return parseInt(this.$moment().format('X'))
        }
        let DateTypeFixTerm = {
          type: 'DATE_TYPE_FIX_TERM',
          fixedTerm: 30,
          fixedBeginTerm: 1,
          endTimestamp: 1489334399
        }
        let DateTypeFixTermRange = {
          type: 'DATE_TYPE_FIX_TIME_RANGE',
          beginTimestamp: 1486857600,
          endTimestamp: 1489334399
        }
        list.push({
          id: i,
          status: _status(0, 5),
          updateTime: _updateTime(),
          weixinCard: i % 2 !== 0,
          title: '优惠券标题' + i,
          startTime: _startTime(1),
          endTime: _endTime(1 + i),
          updateUser: '黄倬, 13262268525',
          cardType: 'CASH',
          sku: {
            quantity: 50000,
            stock: 2000
          },
          dateInfo: i % 2 !== 0 ? DateTypeFixTerm : DateTypeFixTermRange,
          userTagsGet: i % 2 !== 0 ? [] : [{id: i, label: '标签名称' + i}, {id: i + 1, label: '标签名称' + (i + 1)}],
          userTagsConsumed: i % 2 !== 0 ? [] : [{id: i, label: '标签名称' + i}]
        })
        i--
      }
      this.reward.list = list
      this.reward.pages = pages + 1
      this.reward.total = total
      // console.table(this.reward.list)
    }
  }
}
</script>
